package cn.liyicun.cp.cpweb.component.freemark;

import cn.liyicun.cp.cpweb.entity.UserInfo;
import cn.liyicun.cp.cpweb.service.UserInfoService;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserNameMethodModel implements TemplateMethodModelEx {


    @Autowired
    UserInfoService userInfoService;

    @Override
    public Object exec(List arguments) throws TemplateModelException {
        if (arguments == null || arguments.isEmpty()) {
            return "";
        }


        Long uid = Long.valueOf(arguments.get(0).toString());
        UserInfo info = userInfoService.getUserInfo(uid);
        if (info == null) return uid;


        return info.getUserName();
    }
}
